﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLToolkit.Mapping;

namespace Devsoft.DB.Models.Crypto
{
    public class EncryptedAttribute : MemberMapperAttribute
    {
        public EncryptedAttribute()
            : base(typeof(EncryptedMemberMapper))
        {
        }

        public EncryptedAttribute(Type memberType)
            : base(memberType, typeof(EncryptedMemberMapper))
        {
        }
    }

    public class EncryptedMemberMapper : MemberMapper
    {
        public override object ChangeType(object value)
        {
            return CryptoService.Default.Encrypt(value);
        }
        public override void SetValue(object o, object value)
        {
            base.SetValue(o,CryptoService.Default.Decrypt((string)value));
        }

        public override object GetValue(object o)
        {
            return CryptoService.Default.Encrypt(o);
        }
    }
}
